package com.yoomiito.app.ui.anyview.sureorder.header

import android.content.Context
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.my.MyRemainMoneyInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.RoundImageView
import kotlinx.android.synthetic.main.item_recommend_goods.view.*

class SureOrderGoodsIncludeSizeView(context: Context, val detail: GoodsDetail, onShowListener:(spanText:SpannableString,  goodsSize:Int, yoMoneyPay:Int)->Unit={_,_,_->})
    :BaseSureOrderHeaderView(context,onShowListener){
    private var mGoodsPrice="0"

    private lateinit var moneyInfo: MyRemainMoneyInfo
    private val goodsView: View = LayoutInflater.from(mContext).inflate(R.layout.item_goods_13,null,false)

    init {
        goodsView.findViewById<TextView>(R.id.goodsPrice).visibility = View.INVISIBLE
        goodsView.findViewById<RoundImageView>(R.id.goodsPic).let{
            ImageLoader.getInstance().into(mContext,detail.pict_url,it)
        }
        goodsView.findViewById<TextView>(R.id.goodsTitle).text = detail.title
        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        lp.leftMargin=DensityUtil.dp2px(12f)
        lp.rightMargin=DensityUtil.dp2px(12f)
        lp.topMargin=DensityUtil.dp2px(10f)
        addView(goodsView,lp)
        addGoodsSizeView()
        addDeductView()
        addMessageView()
        deductionView.visibility = if (SpannableUtils.isEmptyOr0(detail.deduction_account)) View.GONE else View.VISIBLE
    }

    override fun setMoneyInfo(moneyInfo: MyRemainMoneyInfo){
        this.moneyInfo = moneyInfo
        if (SpannableUtils.isEmptyOr0(detail.deduction_account)) { //如果抵扣优米为0 则不显示
            deductionView.visibility = View.GONE
        } else {
            deductionView.visibility = View.VISIBLE
            deductionView.setYoumiSize(moneyInfo.deduction_account)
        }
        recalculateMoney()
    }

    override fun recalculateMoney() {
        val youMoneyPay: Int
        var youmiSize: String = detail.deduction_account
        when {
            SpannableUtils.isEmptyOr0(detail.deduction_account) -> {
                mGoodsPrice = detail.salePrice //使用商品原价作为价格计算
                youMoneyPay = 0
                youmiSize = "0"
            }
            AllUtils.moneyCompare(moneyInfo.deduction_account, detail.deduction_account) -> { //大于等于
//            mGoodsPrice = mGoodsDetail.getCash();//使用商品扣除优米后的价格作为价格计算
                mGoodsPrice = AllUtils.priceSub_2(detail.salePrice, detail.deduction_account)
                youMoneyPay = 1
            }
            else -> { //小于
                mGoodsPrice = detail.salePrice //使用商品原价作为价格计算
                youMoneyPay = 0
                youmiSize = "0"
            }
        }
        calculatePrice(youmiSize, youMoneyPay)
    }

    private fun calculatePrice(youmiSize: String, youMoneyPay: Int) {
        deductionView.setValue(youmiSize)
        //        mGoodsTotalPrice = AllUtils.priceSub(AllUtils.priceMul_2(mGoodsPrice, goodsSize), String.valueOf(youmiSize));
        L.i("计算付款：$mGoodsPrice  $goodsSize")
        mGoodsTotalPrice = AllUtils.priceMul_2(mGoodsPrice, mGoodsSize)
        L.i("计算付款后的值：$mGoodsTotalPrice")
        val s = "共" + goodsSize + "件 合计¥" + mGoodsTotalPrice
        val p = s.indexOf("¥")
        onShowListener(SpannableUtils.getSizeAndPriceText(s, R.color.color_FF035B, p, s.length, p, p + 1), mGoodsSize, youMoneyPay)
    }
}